package com.controller;

import com.entity.vo.UserResourceResultVO;
import com.entity.vo.UserResourceVo;
import com.github.pagehelper.PageInfo;
import com.service.UserResourceService;
import com.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;


@Controller
@RequestMapping("/resource")
public class ResourseController {

    @Autowired
    UserResourceService userResourceService;

//    "pageNo":page"title":title,user_name":user_name, create_start_date":create_start_date,create_end_date":create_end_date,
//            ""status":status, "file_type":file_type
    @RequestMapping("/findBackResource.do")
    @ResponseBody
    public AjaxResult findBackResource(UserResourceVo userResourceVo){
        PageInfo<UserResourceResultVO> UserResourceResultVOPageInfo = userResourceService.showfindBackResource(userResourceVo);
        System.out.println("findBackResource========"+userResourceService.getClass());
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setObj(UserResourceResultVOPageInfo);
        return ajaxResult;
    }

//    url : '/resource/toggleStatus.do',data : "id":id,"status":status
    @RequestMapping("/toggleStatus.do")
    @ResponseBody
    public Integer toggleStatus(Integer id,Integer status){
        System.out.println("id----"+id+"status=="+status);
        Integer integer = userResourceService.changeToggleStatus(id, status);
        return integer;
    }

    @RequestMapping("/play.do")
    public String play(String path, Map map,String original_name,HttpServletRequest request) throws UnsupportedEncodingException {
//        System.out.println(path+"----------------------------------------------"+"original_name="+original_name);
        String str=path+"/"+original_name+".mp4";
        System.out.println(str);
        map.put("path", str);
//        map.put("path", "/upload/banner.mp4");
        return "background/back_resourceDetailSet";
    }
//    	<a href="/resource/down.do?resourceId={{item.id}}">
    @RequestMapping("/down.do")
    @ResponseBody
    public ResponseEntity<byte[]> down(Integer resourceId,HttpServletRequest request){
        ResponseEntity<byte[]> responseEntity = userResourceService.dowmFile(resourceId, request);
        return responseEntity;
    }
}
